package com.kystar.muxplayer.ui;

import android.graphics.Rect;
import android.inputmethodservice.Keyboard;
import android.view.View;
import android.widget.TextView;

import com.kystar.muxplayer.R;
import com.kystar.muxplayer.base.Strings;
import com.kystar.muxplayer.selfview.MyEditText;
import com.kystar.muxplayer.selfview.MyKeyBoard;

/**
 * Created by zhusheng on 2018-03-10.
 */
public class RectGroupView implements View.OnClickListener {
    public MyEditText left;
    public MyEditText top;
    public MyEditText width;
    public MyEditText height;
    MyKeyBoard mKeyBoard;

    public RectGroupView(View root, MyKeyBoard keyBoard) {
        mKeyBoard = keyBoard;
        left = root.findViewById(R.id.group_rect_left);
        top = root.findViewById(R.id.group_rect_top);
        width = root.findViewById(R.id.group_rect_width);
        height = root.findViewById(R.id.group_rect_height);
        left.setOnClickListener(this);
        top.setOnClickListener(this);
        width.setOnClickListener(this);
        height.setOnClickListener(this);
    }

    public void setRect(Rect rect) {
        left.setText(String.valueOf(rect.left));
        top.setText(String.valueOf(rect.top));
        width.setText(String.valueOf(rect.width()));
        height.setText(String.valueOf(rect.height()));
    }

    public Rect getRect() {
        Rect rect = new Rect();
        getRect(rect);
        return rect;
    }

    public void getRect(Rect rect) {
        try {
            int l = Strings.toInt(left.getText());
            int t = Strings.toInt(top.getText());
            int w = Strings.toInt(width.getText());
            int h = Strings.toInt(height.getText());
            if (w < 0 || w > 8192) {
                throw new IllegalArgumentException("水平大小不能超出范围(0,4000)");
            }
            if (w < 0 || w > 8192) {
                throw new IllegalArgumentException("垂直大小不能超出范围(0,4000)");
            }
            rect.set(l, t, l + w, t + h);
        } catch (Exception ex) {
            throw new IllegalArgumentException("非法数字");
        }
    }

    @Override
    public void onClick(View v) {
        mKeyBoard.show((MyEditText) v);
    }
}
